<?php
class Utility {

	private static $_errorMessage = '';

	public static function getMessage() {
		return self::$_errorMessage;
	}

	private function __construct() {}

	/**
	 * get the user's team
	* @return array
	*/
	public static function getUserTeam( $teamId ){
		$loginUsrId = Login::checkLoginStatus() == Login::USER_LOGGED_IN ? Login::getUserInfo( 'userId' ) : 0;
		if( $loginUsrId <= 0 ){
			return array();
		}
		$team = array();
		try{
			$objTeams = getMultiple( 'team', "createdUserId||$loginUsrId","teamId||$teamId" );
			foreach( $objTeams() as $objTeam ){
				$id = $objTeam->getTeamId();
				$members = array();

				$objMembers = getMultiple( 'teamMember', "teamId||$id" );
				foreach( $objMembers() as $objMember ){
					$name = $objMember->getUserEmail();
					$joined = 'N';
					$status = 'Inactive';
					$userId = $objMember->getUserId();
					if( $userId > 0 ){
						$objUser = getSingle( 'user', $userId );
						if( $objUser->getResultCount() > 0 ){
							$userId = $objUser->getUserId();
							$status = $objUser->getStatus() == 'verified' ? 'Active' : 'Inactive';
							$joined = 'Y';
							$name = $objUser->getName();
						}
					}
					$members[] = array(
						'id' => $userId,
						'name' => $name, 'userEmail' => $objMember->getUserEmail(),
						'joined' => $joined, "status" => $status
					);
				}

				$team = array(
					'id' => $id, 'name' => $objTeam->getTitle(), 
					'status' => ucwords( $objTeam->getStatus() ),
					'desc' => getShortText( $objTeam->getDescription(), 30 ),
					'link' => HOME_PATH  . "admin/teams/nv/id/$id/",
					'members' => $members
				);
			}
			return $team;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/**
	 * get the all user's teams
	* @return array
	*/
	public static function getAllTeams( $selectedTeamId=0, $quantity=10, $pageNumber=1 ){
		$loginUsrId = Login::checkLoginStatus() == Login::USER_LOGGED_IN ? Login::getUserInfo( 'userId' ) : 0;
		if( $loginUsrId <= 0 ){
			return array();
		}
		$teams = array();
		try{
			$objTeams = getMultiple( 'team', "createdUserId||$loginUsrId", 
			"quantity||$quantity", "pageNumber||$pageNumber", 'paging||Y' );
			foreach( $objTeams() as $objTeam ){
				$id = $objTeam->getTeamId();
				$teams[] = array(
					'id' => $id, 'name' => $objTeam->getTitle(),
					'selected' => $selectedTeamId == $id ? 'selected' : '',
					'status' => ucwords( $objTeam->getStatus() ),
					'desc' => getShortText( $objTeam->getDescription(), 30 ),
					'link' => HOME_PATH  . "admin/teams/nv/id/$id/",
					'members' => getMultiple( 'teamMember', "teamId||$id", 'count||Y' )->getTotalCount()
				);
			}
			return $teams;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/**
	 * get the all user's teams
	* @return array
	*/
	public static function getUserTeams( $selectedTeamId=0, $quantity=50, $pageNumber=1 ){
		$loginUsrId = Login::checkLoginStatus() == Login::USER_LOGGED_IN ? Login::getUserInfo( 'userId' ) : 0;
		if( $loginUsrId <= 0 ){
			return array();
		}
		$teams = array();
		try{
			/*$objTeams = getMultiple( 'team', "createdUserId||$loginUsrId", 
			"quantity||$quantity", "pageNumber||$pageNumber", 'paging||Y' );
			foreach( $objTeams() as $objTeam ){
				$id = $objTeam->getTeamId();
				$teams[] = array(
					'id' => $id, 'name' => $objTeam->getTitle(), 
					'selected' => $selectedTeamId == $id ? 'selected' : '',
					'status' => ucwords( $objTeam->getStatus() ),
					'desc' => getShortText( $objTeam->getDescription(), 30 ),
					'link' => HOME_PATH  . "admin/teams/nv/id/$id/",
					'members' => getMultiple( 'teamMember', "teamId||$id", 'count||Y' )->getTotalCount()
				);
			}*/
			$objUserTeams = getMultiple( 'teamMember', "userId||$loginUsrId", 
				'groupBy||teamId', 'status||active', 'joined||Y', 'paging||Y', 
				"quantity||$quantity", "pageNumber||$pageNumber" );
			foreach( $objUserTeams() as $objUserTeam ){
				$teamId = $objUserTeam->getTeamId();
				$objTeam = getMultiple( 'team', 'status||active', "teamId||$teamId" );
				if( $objTeam->getResultCount() == 0 ){
					continue;
				}
				$teams[] = array(
					'id' => $teamId, 'name' => $objTeam->getTitle(), 
					'selected' => $selectedTeamId == $teamId ? 'selected' : '',
					'status' => ucwords( $objTeam->getStatus() ),
					'desc' => getShortText( $objTeam->getDescription(), 30 ),
					'link' => HOME_PATH  . "admin/teams/nv/id/$teamId/",
					'members' => getMultiple( 'teamMember', "teamId||$teamId", 'count||Y' )->getTotalCount()
				);
			}
			return $teams;
		}catch( Exception $ex ){
			throw $ex;
		}
	}


	/**
	 * get the all user's active teams
	* @return array
	*/
	public static function getTeamLists(){
		$loginUsrId = Login::checkLoginStatus() == Login::USER_LOGGED_IN ? Login::getUserInfo( 'userId' ) : 0;
		if( $loginUsrId <= 0 ){
			return array();
		}
		$teams = array();
		try{
			$objTeams = getMultiple( 'team', "createdUserId||$loginUsrId", 'status||active', "quantity||100" );
			foreach( $objTeams() as $objTeam ){
				$id = $objTeam->getTeamId();
				if( getMultiple( 'teamMember', "teamId||$id", 'status||active', 'joined||Y', 'count||Y' )->getTotalCount() == 0 ){
					continue;
				}
				$teams[] = array( 'id' => $id, 'name' => $objTeam->getTitle() );
			}
			return $teams;
		}catch( Exception $ex ){
			throw $ex;
		}
	}


	/**
	 * get the all user's active team's members
	* @return array
	*/
	public static function getTeamMembersLists( $teamId ){
		$loginUsrId = Login::checkLoginStatus() == Login::USER_LOGGED_IN ? Login::getUserInfo( 'userId' ) : 0;
		if( $loginUsrId <= 0 ){
			return array();
		}
		$members = array();
		try{
			$objTeamMembers = getMultiple( 'teamMember', "teamId||$teamId", 
				'status||active', "quantity||100", 'sortColumn||userId', 'sortOrder||asc' );
			foreach( $objTeamMembers() as $objTeamMember ){
				$userId = $objTeamMember->getUserId();
				$objUser = getMultiple( 'user', "userId||$userId", 'status||verified' );
				if( $objUser->getResultCount() == 0 ){
					continue;
				}
				$members[ $userId ] = array( 'id' => $userId, 'text' => $objUser->getName() );
			}
			return $members;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/**
	 * get the all user's active team's members
	* @return array
	*/
	public static function getTeamMembersAndGroupMembers( $teamId, $groupId=0, $memberId=0 ){
		$loginUsrId = Login::checkLoginStatus() == Login::USER_LOGGED_IN ? Login::getUserInfo( 'userId' ) : 0;
		if( $loginUsrId <= 0 ){
			return array();
		}

		$members = array();
		if( $teamId <= 0 ){
			return $members;
		}
		try{
			$objTeamMembers = getMultiple( 'teamMember', "teamId||$teamId", 
				'status||active', "quantity||100", 'sortColumn||userId', 'sortOrder||asc' );
			foreach( $objTeamMembers() as $objTeamMember ){
				$userId = $objTeamMember->getUserId();
				$objUser = getMultiple( 'user', "userId||$userId", 'status||verified' );
				if( $objUser->getResultCount() == 0 ){
					continue;
				}
				$members[ $userId ] = array( 
					'id' => $userId, 'text' => $objUser->getName(),
					'checked' => $memberId == $userId ? 'checked' : ''
				);
			}
			if( $groupId > 0 && $memberId == 0 ){
				// check if group exist or not else create the team group
				$objTeamGroup = getMultiple( 'group', "groupId||$groupId" ,"teamId||$teamId", 'status||active' );
				if( $objTeamGroup->getResultCount() ){
					$groupMembers = explode( ',', $objTeamGroup->getMembers() );
					foreach( $groupMembers as $member ){
						if( isset( $members[ $member ] ) ){
							$members[ $member ][ 'checked' ] = 'checked';
						}
					}
				}
			}
			return $members;
		}catch( Exception $ex ){
			throw $ex;
		}
	}


	/**
	 * add new team
	* @return team id
	*/
	public static function createTeam( $postVars ){
		$loginUsrId = Login::checkLoginStatus() == Login::USER_LOGGED_IN ? Login::getUserInfo( 'userId' ) : 0;
		if( $loginUsrId <= 0 ){
			return false;
		}
		try{
			$title = trim( $postVars[ 'title' ] );
			if( $title == '' ){
				throw new Exception( 'Team name is a required parameter !' );
			}
			$description = trim( $postVars[ 'description' ] );
			$emails = trim( $postVars[ 'emails' ] );
			if( $emails == '' ){
				throw new Exception( 'Team Members is a required parameter !' );
			}
			$emails = explode( ',', $emails );
			$haveValidEmail = false;
			foreach( $emails as $email ){
				if( isValidEmailId( $email ) ) {
					$haveValidEmail = true;
				}
			}
			if( $haveValidEmail == false ){
				throw new Exception( 'Please enter valid email addresses !' );
			}
			// duplicate value check
			$objTeamChk = getMultiple( 'team', "title||$title", 'count||Y' );
			if( $objTeamChk->getTotalCount() > 0 ){
				throw new Exception( 'You already registered team from this name, Try to user another name !' );
			}
			// add team
			$objTeamAdd = getDb( 'team', 'add' );
			$objTeamAdd->set( "title||$title", "description||$description" );
			$newTeamId = $objTeamAdd->getLastInsertedId();
			if( $newTeamId < 1 ){
				throw new Exception( 'Unable to add team !' );
			}
			// add team or invite user to join team
			foreach( $emails as $email ){
				$userId = 0;
				$status = 'inactive';
				$joined = 'N';
				// check if user already member or not if not send mail to request for join the team
				$objUserChk = getMultiple( 'user', "email||$email" );
				if( $objUserChk->getResultCount() > 0 ){
					$userId = $objUserChk->getUserId();
					$status = $objUserChk->getStatus() == 'verified' ? 'active' : 'inactive';
					$joined = 'Y';
				}else{
					// send email to request to joint the team
				}
				// add team member data
				$objTeamMemberAdd = getDb( 'teamMember', 'add' );
				$objTeamMemberAdd->set( "teamId||$newTeamId", "userId||$userId", 
					"userEmail||$email", "status||$status", "joined||$joined" );
			}
		}catch( Exception $ex ){
			throw $ex;
		}
	}

}
